package com.kolon.business.mBoard.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mBoard.controller.BoardSearchListController")
public class BoardSearchListController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mBoard/boardSearchList.do")
	public ModelAndView controller(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String CompanyCode = (String)userMap.get("CompanyCD");
		String page = ComUtil.nullChk(request.getParameter("page"), "1");
		int pageInt = ComUtil.sToi(page) * 15;
		// 하위메뉴가 없을경우
		if("0".equals(request.getParameter("B_IsSub"))) {
			StringBuffer buffer = new StringBuffer();
			buffer.append(url);
			List mapList = new ArrayList();
			Map map = new HashMap();
			if("3".equals(request.getParameter("strMode"))) {
				buffer.append("/board.aspx?strMode=3");
				buffer.append("&BoardRootCD=").append(request.getParameter("BoardRootCD"));;
				buffer.append("&BoardCD=").append(request.getParameter("BoardCD"));
				buffer.append("&SearchScope=C");
				buffer.append("&SearchUserName=");
				buffer.append("&SearchTitle=").append(ComUtil.getEncode(request.getParameter("SearchTitle")==null ? "ㄱ" : request.getParameter("SearchTitle"),"EUC-KR"));
				buffer.append("&SearchStartDate=").append(request.getParameter("SearchStartDate")==null ? "" : request.getParameter("SearchStartDate"));
				buffer.append("&SearchEndDate=").append(request.getParameter("SearchEndDate")==null ? "" : request.getParameter("SearchEndDate"));
				buffer.append("&SortFieldName=SortCreateDate");
				buffer.append("&SortAscDesc=desc");
				buffer.append("&Page=1");
				buffer.append("&CompanyCode=").append(CompanyCode);
				buffer.append("&ViewRowCount=").append(pageInt);
				buffer.append("&userid=").append(id);
				buffer.append("&pw=").append(pw);
				// XML DOCUMENT 및 인증 처리
				Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
				Element element = doc.getRootElement();
				List xmlList = element.getChildren("BoardList");
				Iterator itXml = xmlList.iterator();
				// XML 데이터 
				while (itXml.hasNext()) {
					Element el = (Element)itXml.next();
					map = new HashMap();
					map.put("ArticleCD", el.getChild("ArticleCD").getText());
					map.put("UserID", el.getChild("UserID").getText());
					map.put("UserName", el.getChild("UserName").getText());
					map.put("BoardCD", el.getChild("BoardCD").getText());
					//map.put("Title", ComUtil.subString(el.getChild("Title").getText(), 21));
					map.put("Title", el.getChild("Title").getText());
					map.put("UCreateDate", el.getChild("UCreateDate").getText());
					map.put("ExpireDate", el.getChild("ExpireDate").getText());
					map.put("ViewCnt", el.getChild("ViewCnt").getText());
					map.put("FileOnly", el.getChild("FileOnly").getText());
					map.put("Importance", el.getChild("Importance").getText());
					map.put("AttachCNT", el.getChild("AttachCNT").getText());
					map.put("CommentCNT", el.getChild("CommentCNT").getText());
					map.put("Ref_Seq", el.getChild("Ref_Seq").getText());
					map.put("Ref_Level", el.getChild("Ref_Level").getText());
					map.put("SubCnt", el.getChild("SubCnt").getText());
					map.put("IsNotice", el.getChild("IsNotice").getText());
					map.put("B_ACL", el.getChild("B_ACL").getText());
					mapList.add(map);
				}
			}
			// TableInfo 영역
			Document doc2 = ComUtil.getDocument(buffer.toString(), id, pw);
			Element element2 = doc2.getRootElement();
			List xmlList2 = element2.getChildren("TableInfo");
			Iterator itXml2 = xmlList2.iterator();
			Map map2 = new HashMap();
			// XML 데이터 
			if (itXml2.hasNext()) {
				Element el = (Element)itXml2.next();
				map2 = new HashMap();
				map2.put("TotalRecordCnt", el.getChild("TotalRecordCnt").getText());
				map2.put("TotalNoticeRecortCnt", el.getChild("TotalNoticeRecortCnt").getText());
				map2.put("TotalPage", el.getChild("TotalPage").getText());
				map2.put("CurrPage", el.getChild("CurrPage").getText());
				map2.put("OnePageCnt", el.getChild("OnePageCnt").getText());
				map2.put("bPublicWrite", el.getChild("bPublicWrite").getText());
				map2.put("UserID", el.getChild("UserID").getText());
				map2.put("SAdminCNT", el.getChild("SAdminCNT").getText());
			}
			// 리턴
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.addObject("mapList", mapList);
			modelAndView.addObject("mapList2", map2);
			modelAndView.addObject("strMode", "3");
			modelAndView.addObject("BoardCD", request.getParameter("BoardCD"));
			modelAndView.addObject("SortFieldName", "SortCreateDate");
			modelAndView.addObject("SortAscDesc", "desc");
			modelAndView.addObject("page", page);
			modelAndView.addObject("CompanyCode", CompanyCode);
			modelAndView.addObject("BoardRootCD", request.getParameter("BoardRootCD"));
			modelAndView.addObject("BoardRootCDName", request.getParameter("BoardRootCDName"));
			modelAndView.addObject("BoardName", request.getParameter("BoardName"));
			modelAndView.addObject("B_IsSub", request.getParameter("B_IsSub"));
			modelAndView.addObject("Depth", request.getParameter("Depth"));
			modelAndView.addObject("SearchTitle", request.getParameter("SearchTitle"));
			modelAndView.addObject("SearchStartDate", request.getParameter("SearchStartDate"));
			modelAndView.addObject("SearchEndDate", request.getParameter("SearchEndDate"));
			return modelAndView;
		}else{
			// 하위메뉴가 있을경우
			StringBuffer buffer = new StringBuffer();
			buffer.append(url);
			buffer.append("/board.aspx?strMode=").append("2");
			buffer.append("&UserCompanyCode=").append(CompanyCode);
			buffer.append("&BoardRootCD=").append(request.getParameter("BoardRootCD"));
			buffer.append("&ParentCD=").append(request.getParameter("BoardCD"));
			buffer.append("&userid=").append(id);
			buffer.append("&pw=").append(pw);
			// XML DOCUMENT 및 인증 처리
			Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
			Element element = doc.getRootElement();
			List xmlList = element.getChildren();
			Iterator itXml = xmlList.iterator();
			List mapList = new ArrayList();
			Map map = new HashMap();
			// XML 데이터 
			while (itXml.hasNext()) {
				Element el = (Element)itXml.next();
				map = new HashMap();
				map.put("CompanyCD", el.getChild("CompanyCD").getText());
				map.put("BoardRootCD", el.getChild("BoardRootCD").getText());
				map.put("BoardCD", el.getChild("BoardCD").getText());
				map.put("ParentCD", el.getChild("ParentCD").getText());
				map.put("Depth", el.getChild("Depth").getText());
				map.put("BoardName", el.getChild("BoardName").getText());
				map.put("BoardNameUtf", ComUtil.getEncode(el.getChild("BoardName").getText(),"UTF-8"));
				map.put("B_IsSub", el.getChild("B_IsSub").getText());
				map.put("B_ACL", el.getChild("B_ACL").getText());
				map.put("SAdmin", el.getChild("SAdmin").getText());
				map.put("Password", el.getChild("Password").getText());
				mapList.add(map);
			}
			// 리턴
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.addObject("mapList", mapList);
			modelAndView.addObject("strMode", "3");
			modelAndView.addObject("BoardCD", request.getParameter("BoardCD"));
			modelAndView.addObject("SortFieldName", "SortCreateDate");
			modelAndView.addObject("SortAscDesc", "desc");
			modelAndView.addObject("Page", request.getParameter("Page"));
			modelAndView.addObject("CompanyCode", CompanyCode);
			modelAndView.addObject("BoardRootCD", request.getParameter("BoardRootCD"));
			modelAndView.addObject("BoardRootCDName", request.getParameter("BoardRootCDName"));
			modelAndView.addObject("BoardRootCDNameUtf", ComUtil.getEncode(request.getParameter("BoardRootCDName"),"UTF-8"));
			modelAndView.addObject("BoardName", request.getParameter("BoardName"));
			modelAndView.addObject("BoardNameUtf", ComUtil.getEncode(request.getParameter("BoardName"),"UTF-8"));
			modelAndView.addObject("B_IsSub", request.getParameter("B_IsSub"));
			modelAndView.addObject("Depth", request.getParameter("Depth"));
			return modelAndView;
		}
	}

}